import sys
from ulits.excelclass import Excel
import os
p=os.path.dirname(__file__)
parent_path = os.path.dirname(p)
sys.path.append(p)
file1=parent_path+'/data/users.xls'
file2=parent_path+'/data/userinfo.xls'
class LoginAction():#用户选择注册登录
    def __init__(self):
        self.path=file1
        self.sheetname='用户账号密码'
    def user_register(self,role):#注册
        flog=True
        while flog:
            self.role=role
            self.User_ID=input('请设置你的注册ID(起始为1的11位手机号码):').strip()
            if self.User_ID.isdigit() and len(self.User_ID)==11 and self.User_ID[0]=='1':
                excel=Excel(self.path)
                return_list=excel.returnlist(self.sheetname,self.User_ID)
                msm=return_list[1]
                if len(msm)==0:
                    self.User_Pwd=input('请设置你的登录密码:').strip()
                    user_register_list=[self.User_ID,self.User_Pwd,self.role]
                    excel=Excel(self.path)
                    excel.add_data(user_register_list)     
                    print('注册成功')
                    flog=False
                else:
                    print('已存在,重新设置')
            else:
                print('输入错误重新输入')
    def user_login(self):
        o=1
        flog=True
        while o<=3 and flog:
            Login_ID=input('请输入你的ID:')
            excel=Excel(self.path)
            return_list=excel.returnlist(self.sheetname,Login_ID)
            msy=return_list[0]
            msm=return_list[1]
            if len(msm)==0:
                print('未注册请先注册')
                return False
            else:
                Login_Pwd=input('请输入你的登录密码:')
                list_login=[Login_ID,Login_Pwd]
                if list_login == msm[0][:-1]:
                    chose_user=msm[0][2]#判断角色
                    print('登陆成功')
                    flog=False
                    return Login_ID,chose_user
                elif o<=2:
                        print('登录失败，你还有%d次机会'%(3-o))
                else:
                    print('失败三次账户冻结')
                    exit()
                o+=1  
